package kgame5.kmath {

/**
 * @author Administrator
 * 
 * 产生一个不重复的随机数列
 * 
 */
public class UniqueRandomArray {
	private var posi:int;
	private var aTemp:Array;
	public function UniqueRandomArray(){
		
	}
	public function create(len:int){
		if(len==1){//如果长度是1则直接返回0
			aTemp=[0];
		}else{
			aTemp=GenRandArray(len);
		}
		posi=0;
	}
	public function getNext():int{
		posi++;
		if(posi==aTemp.length){
			create(aTemp.length);//重新生成一下
		}
		return aTemp[posi];
	}
	//如果是len=4,则返回3,0,1,2
	private static function GenRandArray(len:int):Array{
		len=len-1;
		var tempArray:Array = new Array();
		var array:Array = new Array();
		for (var i:int = 0; i<=len; i++) {
			tempArray.push(i);
		}
		var l:int = len;
		while (true) {
			var randNumber:int = Math.floor(Math.random()*(len-0+1))+0;
			var temp:int = tempArray[randNumber];
			if (temp != -1) {
				array.push(temp);
			}
			tempArray[randNumber] = -1;
			if (array.length == l+1) {
				break;
			}
		}
		return array;
	}
}//end of class
}
